﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using PlanningGIS.Win.Controls;


namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public partial class frmFieldCalcu : Office2007Form
    {
        /// <summary>
        /// 
        /// </summary>
        private ITable m_Table;
        /// <summary>
        /// 
        /// </summary>
        private IFeatureSelection pFeatureSelection;
        /// <summary>
        /// 
        /// </summary>
        private int fieldIndex;
        /// <summary>
        /// 
        /// </summary>
        private IRow m_Row;
        /// <summary>
        /// 
        /// </summary>
        private char[] nums = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        /// <summary>
        /// 
        /// </summary>
        /// <param name="layer"></param>
        /// <param name="index"></param>
        public frmFieldCalcu(IFeatureLayer layer, int index)
        {
            InitializeComponent();
            m_Table = (ITable)layer;
            fieldIndex = index;
            InitializeFieldListBox(m_Table);
            IField pField = m_Table.Fields.get_Field(fieldIndex);
            lblField.Text = pField.Name + " =";

            ImageList imageList = new ImageList();
            imageList.ImageSize = new Size(1, 12);
            lstMethod.StateImageList = imageList;

            chkSZ.Checked = true;
            pFeatureSelection = (IFeatureSelection)layer;
            if (pFeatureSelection.SelectionSet != null && pFeatureSelection.SelectionSet.Count > 0)
            {
                chkOnlySel.Enabled = chkOnlySel.Checked = true;
            }
            else
            {
                chkOnlySel.Checked = chkOnlySel.Enabled = false;
            }

            this.lstMethod.MouseClick += (listView2_MouseClick);
        }

        #region ListView事件
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxX_CheckedChanged(object sender, EventArgs e)
        {
            DevComponents.DotNetBar.Controls.CheckBoxX ckBox = (DevComponents.DotNetBar.Controls.CheckBoxX)sender;
            if (ckBox.Checked && ckBox.Text == "数值")
            {
                InitialFunsList(1);
            }
            else if (ckBox.Checked && ckBox.Text == "字符")
            {
                InitialFunsList(2);
            }
            else
            {
                InitialFunsList(0);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void listView2_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                InsertText(lstMethod.SelectedItems[0].Text);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        private void InitialFunsList(int type)
        {
            this.lstMethod.Items.Clear();
            System.Array arr = Enum.GetValues(typeof(Funs));
            if (type == 1)  //数值
            {
                foreach (object o1 in arr)
                {
                    int val = (int)o1;
                    if (val > 0 && val < 10)
                    {
                        lstMethod.Items.Add(o1.ToString() + "( )");
                    }
                }
            }
            else if (type == 2) //字符
            {
                foreach (object o1 in arr)
                {
                    int val = (int)o1;
                    if (val > 10)
                    {
                        lstMethod.Items.Add(o1.ToString() + "( )");
                    }
                }
            }
            else
            { }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="table"></param>
        private void InitializeFieldListBox(ITable table)
        {
            IFields pFields = table.Fields;
            for (int i = 0; i < pFields.FieldCount; i++)
            {
                IField pFld = pFields.get_Field(i);
                //listBox1.Items.Add(pFld.Name);
                ListViewItem item = new ListViewItem(pFld.Name);
                item.Text = pFld.Name;
                lstField.Items.Add(pFld.Name);
                //listView1.vi
            }
            lstField.MouseDoubleClick += listViewEx1_MouseDoubleClick;
            ImageList imageList = new ImageList();
            imageList.ImageSize = new Size(1, 14);
            lstField.StateImageList = imageList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void listViewEx1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lstField.SelectedItems.Count > 0)
            {
                InsertText(string.Format("[{0}]", lstField.SelectedItems[0].Text));
            }
        }
        #endregion

        /// <summary>
        /// 插入文本到表达式中
        /// </summary>
        /// <param name="text"></param>
        private void InsertText(string text)
        {
            text = " " + text.Trim();
            string preText = tbEXP.Text;
            int position = tbEXP.SelectionStart;
            tbEXP.Text = preText.Insert(position, text);
            tbEXP.SelectionStart = position + text.Length;
            tbEXP.Select();
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnValid_Click(object sender, EventArgs e)
        {
            object[] elementsArr = null;
            try
            {
                elementsArr = validate();
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox(this, ex.Message);
                return;
            }
            MsgHelper.ShowInfomationMsgbox(this, "验证成功！");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private object[] validate()
        {
            string expression = this.tbEXP.Text.Replace(" ", "");
            char[] chars = expression.ToCharArray();
            List<object> insStack = new List<object>();

            string tmpStr = "";
            //附带一种验证方式，表达式由变量与运算符依次联接，变量-表达式-变量
            bool Isvar = false;
            for (int i = 0; i < chars.Length; i++)
            {
                char ch = chars[i];
                if (ValidateCalPixl(ch))    //运算符
                {
                    if (!Isvar) throw new Exception(ch + "运算符解析错误！");
                    insStack.Add(ch);
                    Isvar = false;
                }
                else if (ch == '(')
                {
                    insStack.Add(Bracket.LEFT_BRACKET);
                }
                else if (ch == ')')
                {
                    insStack.Add(Bracket.RIGHT_BRACKET);
                }
                else    //变量
                {
                    if (Isvar) throw new Exception("变量需用运算符联接，解析错误！");
                    Isvar = true;
                    if (ch == '"')           //字符串
                    {
                        tmpStr = "";
                        i++;
                        if (SearchNextStr(chars, ref i, ref tmpStr))
                        {
                            insStack.Add(tmpStr);
                        }
                        else
                        {
                            throw new Exception("未检索至结束符 \" ！");
                        }
                    }
                    else if (IsNum(ch))         //数值
                    {
                        double value = 0;
                        if (SearchNextNum(chars, ref i, ref value))
                        {
                            insStack.Add((double)value);
                        }
                        else
                        {
                            throw new Exception("检索到异常数值表达！");
                        }
                    }
                    else if (ch == '[')
                    {
                        string fieldName = "";
                        if (SearchNextFld(chars, ref i, ref fieldName))
                        {
                            FieldN fn = new FieldN();
                            fn.Name = fieldName;
                            insStack.Add(fn);
                        }
                        else
                        {
                            throw new Exception("未检索至结束符\"]\"！");
                        }
                    }
                    else if (IsAlp(ch))
                    {
                        string param = "";
                        Funs pFun = Funs.NULL;
                        if (FunParse(chars, ref i, ref pFun, ref param))
                        {
                            insStack.Add(new KeyValuePair<Funs, string>(pFun, param));
                        }
                        else
                            throw new Exception("函数方法解析错误!");
                    }
                    else
                    {
                        throw new Exception("检索到未知表达式[" + ch + "..]！");
                    }
                }
            }
            return insStack.ToArray();
        }

        /// <summary>
        /// 计算
        /// </summary>
        /// <param name="mExpr"></param>
        /// <returns></returns>
        public object calculate(object[] mExpr)
        {
            Stack stack = new Stack();

            object res = null;
            for (int i = 0; i < mExpr.Length; i++)
            {
                object currElement = mExpr[i];
                if (currElement is double)  //数值型，压入栈
                {
                    stack.Push(currElement);
                }
                else if (currElement is string) //字符串，压入栈（计算时需要注意字符串的计算）
                {
                    stack.Push(currElement);
                }
                else if (currElement is KeyValuePair<Funs, string>)
                {
                    object val = OperatFun(((KeyValuePair<Funs, string>)currElement).Key, ((KeyValuePair<Funs, string>)currElement).Value);
                    stack.Push(val);
                }
                else if (currElement is FieldN)
                {
                    object val = GetValue(m_Row, ((FieldN)currElement).Name);
                    stack.Push(val);
                }
                else if (currElement is char)
                {
                    char ch = (char)currElement;
                    object no1 = stack.Pop();
                    object no2 = stack.Pop();
                    stack.Push(Eval(ch, no1, no2));
                }
                else
                {
                    throw new Exception("出错，未解析的表达式！");
                }
            }
            if (!IsEmpty(stack))
            {
                res = stack.Pop();
            }
            return res;
        }

        /// <summary>
        /// 将中缀表达式转换为后缀表达式
        /// </summary>
        /// <param name="infixExpr"></param>
        /// <returns></returns>
        public object[] convert(object[] infixExpr)
        {
            List<object> output = new List<object>();
            // 创建一个工作栈
            Stack stack = new Stack();

            for (int i = 0; i < infixExpr.Length; i++)
            {
                object currInputElement = infixExpr[i];
                if (currInputElement is double)     //数值型，压入栈
                {
                    output.Add(currInputElement);
                }
                else if (currInputElement is Bracket)//括号，判断优先级
                {
                    Bracket currInputBracket = (Bracket)currInputElement;
                    if (currInputBracket.Equals(Bracket.LEFT_BRACKET))
                    {
                        stack.Push(currInputBracket);
                    }
                    else   //右括号，寻求匹配（左括号）
                    {
                        // 弹出所有的栈元素(运算符)直到遇到(左)括号
                        object stackElement;
                        while (!IsEmpty(stack))
                        {
                            stackElement = stack.Pop();
                            if (stackElement is Bracket)
                                break;
                            else
                                output.Add(stackElement);
                        }
                    }
                }
                else if (currInputElement is char)  //运算符，目前支持二元运算
                {
                    // 弹出所有优先级别高于或等于当前的所有运算符
                    // (直到把满足条件的全部弹出或者遇到左括号)
                    char currOp = (char)currInputElement;
                    while (!IsEmpty(stack))
                    {
                        object stackElement = stack.Peek();
                        if (stackElement is Bracket)
                        {
                            break;
                        }
                        else
                        {
                            char stackOp = (char)stackElement;
                            if (Priority(stackOp) >= Priority(currOp))  // 优先级高于等于当前的，弹出（至输出队列）
                            {
                                stack.Pop();
                                output.Add(stackElement);
                            }
                            else    // 优先级别低于当前的，没有可以弹出的了
                            { break; }

                        }
                    }
                    stack.Push(currInputElement);
                }
                else    //变量
                {
                    output.Add(currInputElement);
                }
            }
            // 将栈中剩下的元素(运算符)弹出至输出队列
            while (!IsEmpty(stack))
            {
                object stackElement = stack.Pop();
                output.Add(stackElement);
            }

            return output.ToArray();
        }

        #region 运算验证方法
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ch"></param>
        /// <param name="ob1"></param>
        /// <param name="ob2"></param>
        /// <returns></returns>
        private object Eval(char ch, object ob1, object ob2)
        {
            object result = null;
            bool IsStrOpera = false;
            if (ob1.GetType() == typeof(string) || ob2.GetType() == typeof(string))
            {
                IsStrOpera = true;
            }
            if (IsStrOpera && (ch == '-' || ch == '*' || ch == '/'))
                throw new Exception("不支持字符串的减、乘、除运算！");
            switch (ch)
            {
                case '+':
                    if (IsStrOpera)
                    {
                        result = ob2.ToString() + ob1.ToString();
                    }
                    else
                    {
                        result = (double)ob1 + (double)ob2;
                    }
                    break;
                case '-':
                    result = (double)ob2 - (double)ob1;
                    break;
                case '*':
                    result = (double)ob2 * (double)ob1;
                    break;
                case '/':
                    result = (double)ob2 / (double)ob1;
                    break;
                default: break;
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="st"></param>
        /// <returns></returns>
        private bool IsEmpty(Stack st)
        {
            return st.Count == 0 ? true : false;
        }

        /// <summary>
        /// 返回运算符的优先级
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private int Priority(char ch)
        {
            int priority;
            switch (ch)
            {
                case '+':
                    priority = 1;
                    break;
                case '-':
                    priority = 1;
                    break;
                case '*':
                    priority = 2;
                    break;
                case '/':
                    priority = 2;
                    break;
                default:
                    priority = 0;
                    break;
            }
            return priority;
        }

        /// <summary>
        /// 
        /// </summary>
        public class Bracket
        {
            /// <summary>
            /// 
            /// </summary>
            private string name;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="name"></param>
            private Bracket(string name)
            {
                this.name = name;
            }
            /// <summary>
            /// 
            /// </summary>
            public static Bracket LEFT_BRACKET = new Bracket("(");
            /// <summary>
            /// 
            /// </summary>
            public static Bracket RIGHT_BRACKET = new Bracket(")");
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public string toString()
            {
                return name;
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private bool ValidateCalPixl(char ch)
        {
            bool pass = false;
            if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
            {
                pass = true;
            }
            return pass;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private bool IsNum(char ch)
        {
            for (int i = 0; i < nums.Length; i++)
            {
                if (ch == nums[i])
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private bool IsAlp(char ch)
        {
            if (ch >= 65 && ch <= 90)
            {
                return true;
            }
            else if (ch >= 97 && ch <= 122)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 检索下一个字匹配符'"'
        /// </summary>
        /// <param name="chars"></param>
        /// <param name="index"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        private bool SearchNextStr(char[] chars, ref int index, ref string str)
        {
            str = "";
            bool pass = false;
            for (int i = index; index < chars.Length; i++)
            {
                char ch = chars[i];
                if (ch == '"')
                {
                    pass = true;
                    index = i;
                    return pass;
                }
                else
                {
                    str += ch;
                }
            }
            return pass;
        }

        /// <summary>
        /// 检索下一个数值型匹配符'0~9'
        /// </summary>
        /// <param name="chars"></param>
        /// <param name="index"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        private bool SearchNextNum(char[] chars, ref int index, ref double val)
        {
            bool pass = false;
            string tmpStr = "";
            for (int i = index; i < chars.Length; i++)
            {
                char ch = chars[i];
                if (IsNum(ch))
                {
                    tmpStr += ch;
                    if (i == chars.Length - 1)  //检索至末尾
                    {
                        pass = true;
                        val = double.Parse(tmpStr);
                        index = i;
                        return pass;
                    }

                }
                else if (ch == ')' || ValidateCalPixl(ch))  //结束标示符
                {
                    pass = true;
                    val = double.Parse(tmpStr);
                    index = i - 1;
                    return pass;
                }
                else
                {
                    //throw err
                }
            }
            return pass;
        }

        /// <summary>
        /// 检索下一个字段边框']'
        /// </summary>
        /// <param name="chars"></param>
        /// <param name="index"></param>
        /// <param name="fname"></param>
        /// <returns></returns>
        private bool SearchNextFld(char[] chars, ref int index, ref string fname)
        {
            bool pass = false;
            fname = "";
            for (int i = index; i < chars.Length; i++)
            {
                char ch = chars[i];
                if (ch == ']')
                {
                    fname += ch;
                    index = i;
                    pass = true;
                    return pass;
                }
                else if (i == chars.Length - 1)
                {
                    //throw err
                }
                else
                {
                    fname += ch;
                }
            }
            return pass;
        }

        /// <summary>
        /// 解析函数方法，较复杂，需提取方法->验证方法->提取参数括号->提取参数[]（解析参数）->返回
        /// </summary>
        /// <param name="chars"></param>
        /// <param name="index"></param>
        /// <param name="fun"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        private bool FunParse(char[] chars, ref int index, ref Funs fun, ref string parameter)
        {
            bool pass = false;
            string funStr = "";
            string tmpStr = "";
            for (int i = index; i < chars.Length; i++)
            {
                char ch = chars[i];
                if (IsAlp(ch))
                {
                    tmpStr += ch;
                }
                else if (ch == '(')
                {
                    funStr = tmpStr;
                    tmpStr = "";
                    object fObject = null;
                    try
                    {
                        fObject = Enum.Parse(typeof(Funs), funStr, true);
                    }
                    catch
                    { }
                    if (fObject == null)
                    {
                        throw new Exception("未知方法<" + funStr + ">!");
                    }
                    fun = (Funs)fObject;
                    parameter = "";
                    i++;
                    if (ParametersParse(chars, ref i, ref parameter))
                    {
                        index = i;
                        return true;
                    }
                    else
                    {
                        throw new Exception("参数检索问题！");
                    }
                }

            }
            return pass;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="chars"></param>
        /// <param name="index"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private bool ParametersParse(char[] chars, ref int index, ref string parameters)
        {
            bool pass = false;
            string val = "";
            for (int i = index; i < chars.Length; i++)
            {
                char ch = chars[i];
                if (ch == ')')
                {
                    index = i;
                    parameters = val;
                    return true;
                }
                else if (i == chars.Length - 1)
                {
                    throw new Exception("未检索到方法结束符[)]！");
                }
                else
                {
                    val += ch;
                }
            }

            return pass;
        }

        /// <summary>
        /// 
        /// </summary>
        private class FieldN
        {
            public string Name;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="row"></param>
        /// <param name="fName"></param>
        /// <returns></returns>
        private object GetValue(IRow row, string fName)
        {
            string name = fName.TrimEnd(']');
            name = name.TrimStart('[');
            int index = m_Table.FindField(name);
            if (index < 0)
            {
                throw new Exception("没有找到字段[" + name + "]");
            }
            object value = row.get_Value(index);
            IField pField = m_Table.Fields.get_Field(index);
            if (pField.Type == esriFieldType.esriFieldTypeDouble || pField.Type == esriFieldType.esriFieldTypeInteger ||
                pField.Type == esriFieldType.esriFieldTypeOID || pField.Type == esriFieldType.esriFieldTypeSingle ||
                pField.Type == esriFieldType.esriFieldTypeSmallInteger)
            {
                return (double)value;
            }
            else
            {
                return (string)value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="func"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        private object OperatFun(Funs func, string param)
        {
            object res = null;
            double pInt;
            string[] paramts = param.Split(',');
            List<object> stack = new List<object>();
            if (paramts.Length == 0)
            {
                throw new Exception("没有提供方法参数！");
            }
            else
            {
                foreach (string p in paramts)
                {
                    if (double.TryParse(p, out pInt))
                    {
                        stack.Add(pInt);
                    }
                    else if (p.StartsWith("[") && p.EndsWith("]"))
                    {
                        stack.Add(GetValue(m_Row, p));
                    }
                    else
                    {
                        stack.Add(p);
                    }
                }
            }
            int num = (int)func;
            if (num > 0 && num < 10 && stack[0] is double)
            {
                double dVal = (double)stack[0];
                switch (func)
                {
                    case Funs.Abs:
                        res = Math.Abs(dVal); break;
                    case Funs.Sin:
                        res = Math.Sin(dVal); break;
                    case Funs.Cos:
                        res = Math.Cos(dVal); break;
                    case Funs.Tan:
                        res = Math.Tan(dVal); break;
                    case Funs.Atn:
                        res = Math.Atan(dVal); break;
                    case Funs.Exp:
                        res = Math.Exp(dVal); break;
                    case Funs.Fix:
                        res = Math.Round(dVal, Convert.ToInt16(stack[1]), MidpointRounding.ToEven); break;
                    case Funs.Int:
                        res = Math.Floor(dVal); break;
                    case Funs.Sqr:
                        res = Math.Sqrt(dVal); break;
                    default: break;
                }
            }
            else if (num > 10)
            {
                string sVal = stack[0].ToString();
                int leng = sVal.Length;
                switch (func)
                {
                    case Funs.Left:
                        int rightPad = Math.Min(Convert.ToInt16(stack[1]), leng);
                        res = sVal.Substring(0, rightPad);
                        break;
                    case Funs.Right:
                        int sIndex = Math.Max(0, leng - Convert.ToInt16(stack[1]));
                        int lgth = Math.Min(Convert.ToInt16(stack[1]), leng);
                        res = sVal.Substring(sIndex, lgth);
                        break;
                    case Funs.Len:
                        res = leng; break;
                    case Funs.Trim:
                        res = sVal.Trim(stack[1].ToString().ToCharArray()); break;
                    case Funs.LTrim:
                        res = sVal.TrimStart(stack[1].ToString().ToCharArray()); break;
                    case Funs.RTrim:
                        res = sVal.TrimEnd(stack[1].ToString().ToCharArray()); break;
                    case Funs.Mid:
                        res = sVal.Substring(Convert.ToInt16(stack[1]), Convert.ToInt16(stack[2]));
                        break;
                    case Funs.LCase:
                        res = sVal.ToLower(); break;
                    case Funs.UCase:
                        res = sVal.ToUpper(); break;
                    default: break;
                }
            }
            else
            { throw new Exception("错误，方法与参数类型不匹配！"); }

            return res;
        }

        /// <summary>
        /// 
        /// </summary>
        private enum Funs
        {
            NULL = 0,
            Abs = 1, Sin = 2, Cos = 3, Tan = 4, Atn = 5, Exp = 6, Fix = 7, Int = 8, Sqr = 9,
            Left = 11, Right = 12, Len = 13, Trim = 14, LTrim = 15, RTrim = 16, Mid = 17, LCase = 18, UCase = 19
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbEXP_TextChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            object[] elementsArr = null;
            try
            {
                elementsArr = validate();
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox(this, ex.Message);
                return;
            }
            if (elementsArr == null || elementsArr.Length <= 0)
                return;
            try
            {
                Progressor pr = new Progressor() { ProgressBar = progressBarX1 };
                object[] output = convert(elementsArr);
                if (chkOnlySel.Checked)
                {
                    pr.SetMaxValue(pFeatureSelection.SelectionSet.Count);                   
                    ICursor pCursor;
                    pFeatureSelection.SelectionSet.Search(null, false, out pCursor);
                    m_Row = pCursor.NextRow();
                    while (m_Row != null)
                    {
                        pr.Step();
                        object val = calculate(output);
                        m_Row.set_Value(fieldIndex, val);
                        m_Row.Store();
                        m_Row = pCursor.NextRow();
                    }
                }
                else
                {
                    int cnt = m_Table.RowCount(null);                    
                    pr.SetMaxValue(cnt);
                    ICursor pCursor = m_Table.Search(null, false);
                    m_Row = pCursor.NextRow();
                    while (m_Row != null)
                    {
                        pr.Step();
                        object val = calculate(output);
                        m_Row.set_Value(fieldIndex, val);
                        m_Row.Store();
                        m_Row = pCursor.NextRow();
                    }
                }
                pr.Stop();
                MsgHelper.ShowInfomationMsgbox(this, "完成计算");
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox(this, ex.Message);
            }
        }

        /// <summary>
        /// 符号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFH_Click(object sender, EventArgs e)
        {
            DevComponents.DotNetBar.ButtonX button = (DevComponents.DotNetBar.ButtonX)sender;
            InsertText(button.Text);
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

    }
}
